package com.mystyle.le.weekproject.fragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.activity.DetialSortActivity;
import com.mystyle.le.weekproject.adapter.SoftAdapter;
import com.mystyle.le.weekproject.adapter.SoftImageAdapter;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.beans.Seniority;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;
import com.mystyle.le.weekproject.widget.FlowLayoutView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 搜索的页面
 * A simple {@link Fragment} subclass.
 */
public class SoftFragment extends Fragment {


    private EditText soft_edit;
    private Button sort_button;
    private GridView soft_RG;
    private TextView soft_change;
    private GridView soft_GV;

    public SoftFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_soft, container, false);
        //初始化控件
        initView(view);
        //初始化数据
        initData();
        //点击搜索后跳转检索
        initPosition();
        return view;
    }

    private void initPosition() {

        sort_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = soft_edit.getText().toString().trim();
                Intent intent = new Intent(getActivity(), DetialSortActivity.class);
                intent.putExtra("trim",trim);
                startActivity(intent);
            }
        });
    }

    private void initData() {

        /*
        * 产生的热门搜索
        * */
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<String> list = ParseJson.parseNameJson(path);
                if (list!=null&&list.size()>0){
                    for (int i = 0;i<list.size();i++){
                        SoftAdapter softAdapter = new SoftAdapter(list, getActivity());
                        soft_RG.setAdapter(softAdapter);
                    }
                }
            }
        },getActivity()).execute(Url.SOFT_GRIDVIEW);

        /*
        * 随机产生的三个图片
        * */
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                if (seniorityList!=null&&seniorityList.size()>0){
                    SoftImageAdapter softImageAdapter = new SoftImageAdapter(seniorityList, getActivity());
                    soft_GV.setAdapter(softImageAdapter);
                }
            }
        }, getActivity()).execute(Url.SOFT_CHANGEVIEW);
        /*
        * 点击换一换更新UI
        * */
        soft_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                        if (seniorityList!=null&&seniorityList.size()>0){
                            SoftImageAdapter softImageAdapter = new SoftImageAdapter(seniorityList, getActivity());
                            soft_GV.setAdapter(softImageAdapter);
                        }
                    }
                }, getActivity()).execute(Url.SOFT_CHANGEVIEW);
            }
        });
    }

    /*
    * 初始化控件
    * */
    private void initView(View view) {
        soft_edit = ((EditText) view.findViewById(R.id.soft_edit));
        sort_button = ((Button) view.findViewById(R.id.sort_button));
        soft_RG = ((GridView) view.findViewById(R.id.soft_RG));
        soft_change = ((TextView) view.findViewById(R.id.soft_change));
        soft_GV = ((GridView) view.findViewById(R.id.soft_GV));

    }

}
